package club.twelveblog.tblog.service;

import club.twelveblog.tblog.common.config.AliOssClient;
import club.twelveblog.tblog.common.config.GlobalConfig;
import club.twelveblog.tblog.common.enums.ExceptionEnum;
import club.twelveblog.tblog.common.exception.BlogException;
import club.twelveblog.tblog.dao.AdminDao;
import club.twelveblog.tblog.dao.CategoryDao;
import club.twelveblog.tblog.dao.UserDao;
import club.twelveblog.tblog.entity.Admin;
import club.twelveblog.tblog.entity.Category;
import club.twelveblog.tblog.entity.User;
import club.twelveblog.tblog.utils.DateUtils;
import club.twelveblog.tblog.utils.ReNameImage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class AdminService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private CategoryDao categoryDao;
    private static final List<String> ALLOW_IMAGE_TYPE = Arrays.asList("image/jpeg", "image/jpg", "image/png", "image/gif");
    /**
     * 管理员头像上传
     * @param file
     * @return
     */
    public String UploadAdminImg(MultipartFile file) {
        //用于存储图片访问路径
        String url = "";
        try {
            //校验文件类型
            String contentType = file.getContentType();
            if(!ALLOW_IMAGE_TYPE.contains(contentType)){
                throw new BlogException(ExceptionEnum.INVALID_FILE_TYPE);
            }
            //校验文件内容
            BufferedImage image = ImageIO.read(file.getInputStream());
            if (image == null) {
                throw new BlogException(ExceptionEnum.INVALID_FILE_TYPE);
            }
            /// 创建新实例
            AliOssClient client = new AliOssClient();
            // 连接需要的信息
            client.setAccessKeyId(GlobalConfig.ACCESSKEYID);
            client.setAccessKeySecret(GlobalConfig.ACCESSKEYSECRET);
            client.setEndpoint(GlobalConfig.ENDPOINT);
            //调用重命名方法
            String fileName = ReNameImage.renameFile(file);
            // 获得文件流
            InputStream inputStream = file.getInputStream();
            //上传到OSS
            client.putObject(GlobalConfig.BUCKETNAME, "authorImg/" + fileName, inputStream);
            System.out.println("图片下载url是:" + url);
            url += "https://tblog.oss-cn-beijing.aliyuncs.com/authorImg/" + fileName;
            return url;
        } catch (IOException e) {
            //上传失败
            log.error("上传图片失败", e);
            throw new BlogException(ExceptionEnum.UPLOADIMAGE_ERROR);
        }
    }


    /**
     * 根据admin_id查询指定的管理员
     * @param admin_id
     * @return
     */
    public Optional<Admin> findAdminById(Long admin_id) {

        return adminDao.findById(admin_id);

    }

    /**
     * 根据admin_id修改管理员个人信息
     * @param admin
     */
    @Transactional
    public void upDateAdminInfo(Admin admin) {
        //只修改管理员昵称以及密码
        Admin updateAdmin = adminDao.getOne(admin.getAdmin_id());

//        updateAdmin.setAdmin_info_img(admin.getAdmin_info_img());

        updateAdmin.setAdmin_nick_name(admin.getAdmin_nick_name());

        updateAdmin.setAdmin_password(admin.getAdmin_password());
        adminDao.save(updateAdmin);
    }

    /**
     * 添加分类
     * @param category
     * @return
     */
    @Transactional
    public Category addCategory(Category category) {
        category.setCreate_time(DateUtils.getLocalCurrentDate());
        Category category1 = categoryDao.save(category);
        return category1;
    }

    /**
     * 更新管理员头像Url
     * @param admin
     */
    @Transactional
    public void updateAdminImg(Admin admin) {
        Admin admin1 = adminDao.getOne(admin.getAdmin_id());
        admin1.setAdmin_info_img(admin.getAdmin_info_img());
        adminDao.save(admin);
    }
    /**
     * 管理员登录
     */
    public Admin adminLogin(String admin_name,String admin_password){
        Admin admin =adminDao.findByAdmin(admin_name);
        if (admin!=null){
            boolean a=admin.getAdmin_password().equals(admin_password);
            if (a!=true){
                return null;
            }
            else {
                return admin;
            }
        }else {
            return null;
        }
    }
}
